package com.example.newleadcent;

import com.example.newleadcent.netty.TcpServer;
import com.example.newleadcent.util.FormatUtil;
import com.ruoyi.common.security.annotation.EnableCustomConfig;
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;

import java.net.InetSocketAddress;

@EnableCustomConfig
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringCloudApplication
public class NewleadcentApplication implements CommandLineRunner {      //预加载
//    int port = 35260;
    int port = 25260;

    @Autowired
    private TcpServer tcpServer;

    public static void main(String[] args) {

        SpringApplication.run(NewleadcentApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

//        String s = "#1";
//        String r = checksum(s);
//        System.out.println("r: "+r);
//        int i = FormatUtil.checksum(s);
//        System.out.println("i; "+i);


//        StringBuffer wifi_Data_bf = new StringBuffer(new String("349672816a485400000000000000"));
//        for(int i=wifi_Data_bf.length()-1; i>0; i--){                           //最后一个位置不用插
//            if(i%14 == 0)wifi_Data_bf.insert(i,"|");
//        }
//        System.out.println("wifi: "+wifi_Data_bf);


        System.out.println("TCPSERVER启动");

        InetSocketAddress address = new InetSocketAddress( port );
        ChannelFuture future = tcpServer.start(address);

        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                tcpServer.destroy();
            }
        });

        future.channel().closeFuture().syncUninterruptibly();




    }

    static String checksum(String input){
        char[] chars = input.toCharArray();
//        int a = chars[0];
//        System.out.println("a: "+a);
        int sum = chars[0]^chars[1];        //00110001(49) ^ 00110010(50) = 00000011(3)     字符通过ASC码表找到对应的二进制
        for(int i=2;i<chars.length;i++){
            sum = sum^chars[i];
        }
        System.out.println("sum: "+sum);            //这里不转化输出是10进制
        return String.format("%02x", sum);      //就是各字节异或，最后得到一个8位二进制，转化为2位16进制，再化为ASC字符
    }



}
